<link rel="stylesheet" href="${baseStaticUrl}/plugins/ztree/css/zTreeStyle/zTreeStyle.css" type="text/css">
<script type="text/javascript" src="${baseStaticUrl}/plugins/ztree/js/jquery.ztree.core.js"></script>
<script type="text/javascript" src="${baseStaticUrl}/plugins/ztree/js/jquery.ztree.excheck.js"></script>
<div class="form-container">
	<div class="box">
        <form action="#" method="post" name="suvForm" id="suvForm">
        	<div class="row visa-row">
                <div id="wait-msg" class="alert alert-info"
                     style="background:url(${baseStaticUrl}/images/ajax-loader.gif) no-repeat scroll 0.5em 50%;display:none"></div>
                <div id="alert-msg" class="alert alert-success" style="display:none"></div>
                <div id="error-msg" class="alert alert-danger" style="display:none"></div>
            </div>
            <div class="row visa-row">
            	<h6 class="mg-b-20 h6-mar-sm">
                    <strong>部门管理</strong>
                </h6>
                <div class="row w-row">
                    <div class="col-md-1 col-xs-12 w-row1"><span class="span_red">*</span>组织编码</div>
                    <div class="col-md-3 col-xs-12 w-row2 colself"><input type="text" class="form-control form-control-sm"
                          name="sysOrg.ORG_CODE" id="zzbm" onchange="checkOrgCode()"
                          value="${sysOrg.ORG_CODE!}">
                    </div>
                    <div class="col-md-1 col-xs-12 w-row1"><span class="span_red">*</span>组织名称</div>
                    <div class="col-md-3 col-xs-12 w-row2 colself">
                         <input type="text" class="form-control form-control-sm" id="zzmc" onchange="checkOrgName()"
                                name="sysOrg.ORG_NAME" value="${sysOrg.ORG_NAME!}">
                         <input type="hidden" name="sysOrg.ID" value="${sysOrg.ID!}" id="sysOrgId">
                     </div>
                     <div class="col-md-1 col-xs-12 w-row1"><span class="span_red">*</span>上级组织</div>
                     <div class="col-md-3 col-xs-12 w-row2 colself">
                         <input type="hidden" class="form-control form-control-sm"
                                name="sysOrg.PARENT_ID"
                                value="${parentId!sysOrg.PARENT_ID}">
                         <div class="tdself">
	                         <input type="text" class="form-control form-control-sm" readonly="readonly"
	                                value="${parentName!sysOrg.PARENT_ID}">
	                         <span class="btn visa-btn-icon visa-btn-select fa fa-search fa-1" data-toggle="modal"
		                                data-target="#selectDeptModal"
		                                aria-hidden="true"></span>
                         </div>
                     </div>
                 </div>
                 <div class="row w-row">
                    	<div class="col-md-1 col-xs-12 w-row1">组织简称</div>
                        <div class="col-md-3 col-xs-12 w-row2 colself">
                            <input type="text" class="form-control form-control-sm"
                                   name="sysOrg.SHORT_NAME" value="${sysOrg.SHORT_NAME!}">
                        </div>
                        <div class="col-md-1 col-xs-12 w-row1"><span class="span_red">*</span>状态</div>
                        <div class="col-md-3 col-xs-12 w-row2">
                            <epoch:radio name="sysOrg.STATUS" id="zzzt" list="${lisFun.getDictCode('PUB_ENABLE_DISABLE')}"
                            checked="${sysOrg.STATUS!=null ? sysOrg.STATUS:'ENABLE'}"></epoch:radio>
                        </div>
                        <div class="col-md-1 col-xs-12 w-row1"><span class="span_red">*</span>类型</div>
                        <div class="col-md-3 col-xs-12 w-row2 colself">
                            <epoch:select list="${lisFun.getDictCode('SYS_ORG_TYPE')}"
                                       value="${sysOrg.ORG_TYPE!}" name="sysOrg.ORG_TYPE">
                            </epoch:select>
                        </div>
                 </div>
            </div>
        </form>
    </div>
    <div class="box" id="orgUserDiv">
    	<div class="row range">
    		<epoch:table id="sysOrgUserTable" tableName="分配用户" queryUrl="${basePath}/sys/org/findSysUserByOrgId?org_id=${sysOrg.ID!'-1'}"
            rowId="user_id" newBtn="true" newBtnText="分配用户" delBtn="true" delBtnText="删除" showRefresh="true" autoAdjustWidth="false" onAddClick="addClick" onDelClick="delClick">
	            <epoch:tr check="checkbox" titleTip="全选"></epoch:tr>
	            <epoch:tr name="用户名" formName="account"></epoch:tr>
	            <epoch:tr name="中文姓名" formName="user_name"></epoch:tr>
	            <epoch:tr name="邮箱" formName="email"></epoch:tr>
	            <epoch:tr name="联系方式" formName="phone_number"></epoch:tr>
	            <epoch:tr name="部门" formName="name"></epoch:tr>
	            <epoch:tr name="操作" switchable="false">
	                <button class="btn btn-danger btn-xs" onclick="delRows('~{row.user_id}');">
	                	<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>删除
			        </button>
	            </epoch:tr>
        	</epoch:table>
        	<epoch:winOpen id="selectUserModal" title="分配用户" backdrop="true" width="1280px">
	            <epoch:table id="sysUserTable" queryUrl="${basePath}/sys/user/findUserListAjax" rowId="id" autoAdjustWidth="false" delBtn="false" showRefresh="true"
	                defTipColHead="true">
	                <epoch:tr check="checkbox" titleTip="全选"></epoch:tr>
	                <epoch:tr name="用户名" formName="account"></epoch:tr>
	                <epoch:tr name="中文姓名" formName="user_name"></epoch:tr>
	                <epoch:tr name="邮箱" formName="email"></epoch:tr>
	                <epoch:tr name="员工编号" formName="employee_number"></epoch:tr>
	                <epoch:tr name="联系方式" formName="phone_number"></epoch:tr>
	                <epoch:tr name="所属公司" formName="company_name"></epoch:tr>
	                <epoch:tr name="部门" formName="dept_name"></epoch:tr>
	                <epoch:tr name="是否锁定" formName="is_locked" filterType="select" filterData="${lisFun.dictForFilter('PUB_Y_N')}"></epoch:tr>
	                <epoch:tr name="是否有效" formName="is_used" filterType="select" filterData="${lisFun.dictForFilter('PUB_Y_N')}"></epoch:tr>
	                <epoch:tr name="员工状态" formName="status" filterType="select" filterData="${lisFun.dictForFilter('SYS_USER_STATUS')}"></epoch:tr>
	            </epoch:table>
        	</epoch:winOpen>
    	</div>
    </div>
</div>
<epoch:winOpen id="selectDeptModal" title="选择父级菜单" backdrop="true"
            width="380px" height="550px">
    <div style="width:100%;height:100%;overflow-y:scroll;">
        <ul id="sysZrTreeBean" class="ztree"></ul>
    </div>
</epoch:winOpen>
<script type="text/javascript">
	
	_headToolBar.saveBtn(null, null, saveOrUpdate);
	
	//菜单组织树
	var zTreeObj;
	var setting = {
	    check: {chkStyle: "radio", enable: true, radioType: "all"},
	    data: {simpleData: {enable: true}},
	    callback: {onCheck: zTreeOnClick}
	};
	var zNodes = [{
	    name: "组织结构树",
	    id: "-1",
	    open: true,
	    children:${treeNodeJson}
	}];
	$(document).ready(function () {
	    zTreeObj = $.fn.zTree.init($("#sysZrTreeBean"), setting, zNodes);
	});
	var ids = "";
	var idsName = "";
	function zTreeOnClick(event, treeId, treeNode) {
	    ids = treeNode.id;
	    idsName = treeNode.name;
	}
	
	var flag = false;
	function saveOrUpdate() {
        if ($("#zzbm").val() == null || $("#zzbm").val() == '') {
            showErrorMessage("error-msg", "组织编码");
            $("#zzbm").focus();
            return;
        }
        if ($("#zzmc").val() == null || $("#zzmc").val() == '') {
            showErrorMessage("error-msg", "组织名称");
            $("#zzmc").focus();
            return;
        }
        var flag = true;
        $("#suvForm select").each(function(index, ele) {
            var selectedValue = this.options[this.selectedIndex].value;
            if (isEmpty(selectedValue)) {
                flag = false;
                $(this).focus();
            }
        });
        
        if (!flag) {
            showErrorMessage("error-msg", "类型");
            return;
        }
        clearMessage();
        $.ajax({
            url: "${basePath}/sys/org/saveOrUpdate",
            data: $("form").serialize(),
            type: "post",
            success: function(result) {
            	debugger
                if (result) {
                    if (result.code == "0") {
                        $("#sysOrgId").val(result.data);
                        $("#allotorgUserId").css('display', 'block');
                        MessageBox.success("保存成功。");
                        _tabs.showPage("list");
                    } else {
                        MessageBox.error("保存失败！");
                    }
                } else {
                    MessageBox.error("保存失败！");
                }
            },
            error: function() {
                MessageBox.error("服务器错误，请稍后再试！");
            },
            async: false
        });
	}
	
	$(document).ready(function() {
        if (isEmpty($("#sysOrgId").val())) {
            $("#orgUserDiv").hide();
        } else {
            $("#orgUserDiv").show();
        }
    });
	
	$("#selectUserModal_save").click(function() {
        var selected = $("#sysUserTable").bootstrapTable("getAllSelections");
        if (!isEmpty(selected)) {
            var userId = [];
            for ( var i in selected) {
                userId.push(selected[i].id);
            }
            var orgId = $("#sysOrgId").val();
            $.post("${basePath}/sys/org/saveSysOrgUser", {
                userId: userId,
                orgId: orgId
            }, function(result) {
                if (result.code == 0) {
                    $("#selectUserModal").modal("hide");
                    var opt = {
                        url: "${basePath}/sys/org/findSysUserByOrgId?org_id=" + orgId,
                        silent: true
                    };
                    $("#sysOrgUserTable").bootstrapTable("refresh", opt);
                } else {
                    MessageBox.error("${lisFun.escapeForQuot(i18n('cmn_msg_err_saveFailed'))}")
                }
            })
        }
    });
	
	function orgUser() {
        $("#orgUserDiv").show();
    }
    function addClick() {
        $("#selectUserModal").modal("show");
    }
    function delRows(id){
    	 var orgId = $("#sysOrgId").val();
    	 CommonUtil.del(null, id, "${basePath}/sys/org/deleteUser?org_id=" + orgId, null, function() {
             $("#selectUserModal").modal("hide");
             var opt = {
                 url: "${basePath}/sys/org/findSysUserByOrgId?org_id=" + orgId,
                 silent: true
             };
             $("#sysOrgUserTable").bootstrapTable("refresh", opt);
         });
    }
    function delClick(rows){
    	var orgId = $("#sysOrgId").val();
    	CommonUtil.del(rows, "user_id", "${basePath}/sys/org/deleteUser?org_id=" + orgId, null, function() {
            $("#selectUserModal").modal("hide");
            var opt = {
                url: "${basePath}/sys/org/findSysUserByOrgId?org_id=" + orgId,
                silent: true
            };
            $("#sysOrgUserTable").bootstrapTable("refresh", opt);
        });
    }
</script>